const wxHttpHandler = require('../../utils/wxHttpHandler.js');
const api = require('../../APIConfig/apiConfig.js');
var app = getApp();

Page({
  data: {
    latitude: -1,
    longitude: -1,
    markers: []
  },

  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(this.data.markers[e.detail.markerId])
    wx.navigateTo({
      url: '../searchResult/searchResult?keyWords='+this.data.markers[e.detail.markerId].name+'&indicatorIndex=2&map=true',
    })
  },
  controltap(e) {
    console.log(e.detail.controlId)
  },

  onLoad: function (options) {
    var that = this;
    this.setData({
        date: this.data.year + '-' + this.data.month + '-' + this.data.day,
      }),
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude
          })
          that.queryLogLatList()
        }
      })
  },

  onReady: function (e) {
    this.shakeFun();
  },


  /**
   * 摇一摇
   */
  shakeFun: function () {
    var numX = 1 //x轴
    var numY = 1 // y轴
    var numZ = 0 // z轴
    var stsw = true // 开关，保证在一定的时间内只能是一次，摇成功
    var positivenum = 0 //正数 摇一摇总数
    var audioCtx = wx.createInnerAudioContext()
    wx.onAccelerometerChange(function (res) {
      if (numX < res.x && numY < res.y) {
        positivenum++
        setTimeout(() => {
          positivenum = 0
        }, 2000) //计时两秒内没有摇到指定次数，重新计算
      }
      if (numZ < res.z && numY < res.y) { //可以上下摇，上面的是左右摇
        positivenum++
        setTimeout(() => {
          positivenum = 0
        }, 2000) //计时两秒内没有摇到指定次数，重新计算
      }
      if (positivenum == 2 && stsw) { //是否摇了指定的次数，执行成功后的操作
        stsw = false

        audioCtx.autoplay = true
        audioCtx.src = 'http://pic.ibaotu.com/00/43/58/92J888piCmbi.mp3'
        wx.vibrateLong({
          complete: (res) => {},
        })
        audioCtx.onPlay(() => {
          console.log('播放声音')
        })
        audioCtx.play() //播发音频

        setTimeout(() => {
          positivenum = 0 // 摇一摇总数，重新0开始，计算
          stsw = true
        }, 2000)
      }
    })
  },


  //附近站点
  queryLogLatList: function () {
    let data = {
      latitude: this.data.latitude, // 38.420005
      longitude: this.data.longitude //115.841706

    }
    const that = this
    wxHttpHandler({
      type: 'get',
      path: api.queryLogLatList,
      data: data,
      fn: function (res) {
        if (res.code == 0) {
          if (res.data != null && res.data.length > 0) {
            console.log(res.data.length + '')
            let i
            let marksTem = []
            for (i = 0; i < res.data.length; i++) {
              marksTem.push({
                id:i,
                iconPath: "../../assets/images/queryStation/mark_station@3x.png",
                latitude: res.data[i].latitude,
                longitude: res.data[i].longitude,
                stationId:res.data[i].stationId,
                name:res.data[i].name,
                width: 25,
                height: 30,
                anchorX: 0,
                anchorY: 0,
              })
            }
            that.setData({
              markers:marksTem
            })
          }

        } else {
          wx.showToast({
            title: res.desc,
            icon: 'none',
            duration: 1500,
            mask: true
          });
        }
      }

    })
  },
})